from pptx.util import Inches, Pt
import util,math,sys
from pptx.dml.color import RGBColor
from PIL import Image
import pandas as pd
import numpy as np
from io import BytesIO

def create_layout_ppt(prs,batch,title):
    if len(batch)==0:
        return
    slide = prs.slides.add_slide(prs.slide_layouts[6])
    util.add_text_box(slide,0,0,prs.slide_width,Inches(0.25),title,18,True)
    # 获取画布宽高
    slide_height = prs.slide_height/914400
    slide_width = prs.slide_width/914400
    left_margin = Inches(0.15)
    top_margin = Inches(0.6)

    batch_pd = pd.DataFrame(batch)
    #删除所有空列
    batch_pd = batch_pd.dropna(axis=1,how='all')
    #首行默认标题
    header = batch_pd.iloc[0].reset_index(drop=True)
    batch_pd = batch_pd.iloc[1:].reset_index(drop=True)
    #行列倒置
    batch_pd = batch_pd.transpose()
    batch_pd = batch_pd.dropna(axis=1,how='all')
    rows,cols = batch_pd.shape

    table_width = Inches(slide_width)-left_margin*2
    table_height = Inches(slide_height)
    table = slide.shapes.add_table(rows, cols, left_margin, top_margin, table_width, table_height).table
    util.set_table_border(table)
    util.set_table_background(table)
    # 数据
    for row in range(rows):
        table.rows[row].height = Inches(0.1)
        for col in range(cols):
            cell = table.cell(row, col)
            val = batch_pd.iat[row, col]
            #如果val包含fail 则转为fail
            if "fail" in str(val).lower():
                val = "fail"
            if "normal" in str(val).lower():
                val = "pass"
            cell.text = str(val)
            if row == 0:
                cell.fill.solid()
                cell.fill.fore_color.rgb =  RGBColor(255, 165, 50)
            if col == 0:
                table.columns[col].width = Inches(0.65)
        util.set_table_row_font_size(table,row,10)